package com.hl.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.dao.ShopDaoimpl;
import com.hl.pojo.PanghuShop;
import com.hl.util.GeneralResult;
import com.hl.util.HttpUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/shop/addShop")
public class ShopAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数字符串（将io流转为json字符串）
        String json = HttpUtil.getJson(req);
        //将json字符串转化为java对象
        PanghuShop panghuShop = JSONObject.parseObject(json, PanghuShop.class);
        int num = 0;
        ShopDaoimpl dao = new ShopDaoimpl();
        dao.save(panghuShop);
        //将数据返回给前端
        resp.setContentType("application/json;charset=utf-8");
        String msg = num >0?"操作成功":"操作失败";
        //JSON.toJSONString 将json数据转化为字符串
        resp.getWriter().append(JSON.toJSONString(new GeneralResult(200,msg)));
    }
}
